export default {
    props: {
        // 是否显示自定义检索栏
        showFilter: {
            type: Boolean,
            default: true
        }
    },
    data() {
        return {
            filters: [], // 自定义检索条件
            selectFilterId: 0, // 自定义检索条件默认选项
            filterConditions: []
        }
    },
    computed: {

    },
    methods: {
        initFiltersData(is_refresh = false) {
            if (this.showFilter) {
                this.getFilters({ form_id: this.formId, is_refresh }).then(data => {
                    this.filters = [{ id: 0, title: '全部', conditions: [], is_system: 1 }].concat(data);
                });
            }
        },
        // 删除自定义查询条件
        handleDeleteFilter: function (filterId, name) {
            this.$Modal.confirm({
                title: '确认',
                content: `确认删除【${name}】?`,
                onOk: () => {
                    this.deleteFilters({ filter_id: filterId }).then(res => {
                        this.initFiltersData(true);
                    });
                }
            });
        },
        // 查询条件变更
        handleClickFilter: function (selectFilter) {
            let conditions = selectFilter.conditions.map(c => {
                return {
                    field_name: c.field_name,
                    type: c.type,
                    value: c.value,
                    field_type: c.field_type
                };
            });

            // 进行中
            if (selectFilter.id == 11 || selectFilter.id == 20) {
                conditions = [{
                    field_name: 'stime',
                    type: 8,
                    value: Math.floor(Date.parse(new Date()) / 1000),
                    field_type: 'datetime'
                },
                {
                    field_name: 'etime',
                    type: 6,
                    value: Math.floor(Date.parse(new Date()) / 1000),
                    field_type: 'datetime'
                }
                ]
            }

            // 未开始
            if (selectFilter.id == 12 || selectFilter.id == 21) {
                conditions = [
                    {
                        field_name: 'stime',
                        type: 6,
                        value: Math.floor(Date.parse(new Date()) / 1000),
                        field_type: 'datetime'
                    }
                ]
            }

            // 已结束
            if (selectFilter.id == 13 || selectFilter.id == 22) {
                conditions = [
                    {
                        field_name: 'etime',
                        type: 8,
                        value: Math.floor(Date.parse(new Date()) / 1000),
                        field_type: 'datetime'
                    }
                ]
            }

            this.selectFilterId = selectFilter.id || 0;
            this.filterConditions = conditions;

            this.refreshDate();
            // this.$emit("on-filter-click", selected);
        },
        // 保存自定义查询条件
        handleSaveFilter: function (data, success, error) {
            if (data.id) {
                this.updateFilters({
                    filter_id: data.id,
                    filter: data
                })
                    .then(res => {
                        this.initFiltersData(true);
                        success();
                    })
                    .catch(err => {
                        error(err);
                    });
            } else {
                this.addFilters({
                    form_id: this.formId,
                    filter: data
                })
                    .then(res => {
                        this.initFiltersData(true);
                        success();
                    })
                    .catch(err => {
                        error(err);
                    });
            }
            // this.$emit("on-save-filter", data, success, error);
        }
    }
};